package com.wufeng.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wufeng.dto.KnowledgeUserDto;
import com.wufeng.dto.R;
import com.wufeng.entity.Knowledge;
import com.wufeng.entity.User;
import com.wufeng.service.KnowledgeService;
import com.wufeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {


    @Autowired
    private KnowledgeService knowledgeService;


    @Autowired
    private UserService userService;

    /**
     * @return
     */
    @GetMapping("/getKnowledgeList")
    public R getKnowledgeList(@RequestParam("isCarousel") Integer isCarousel) {

        QueryWrapper<Knowledge> queryWrapper = new QueryWrapper<Knowledge>().eq("isCarousel", isCarousel);
        List<Knowledge> list = knowledgeService.list(queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", list);
        return R.ok(map);
    }


    /**
     * 根据id获取知识
     *
     * @param id
     * @return
     */
    @GetMapping("/getKnowledgeById")
    public R getKnowledgeById(@RequestParam("id") Integer id) {
        QueryWrapper<Knowledge> queryWrapper =
                new QueryWrapper<Knowledge>().eq("knowledgeId", id);
        Knowledge knowledge = knowledgeService.getOne(queryWrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("knowledge", knowledge);
        return R.ok(map);
    }


    /**
     * 添加心理知识
     *
     * @param knowledge
     * @return
     */
    //上面可能要改
    @PostMapping("/addKnowledge")
    public R addKnowledge(@RequestBody Knowledge knowledge) {
        System.out.println(knowledge.toString());
        Date date = new Date();
        knowledge.setCreatetime(date);
        knowledge.setUpdatetime(date);
        knowledgeService.save(knowledge);
        return R.ok();
    }


    /**
     * 根据条件查询心理知识列表
     *
     * @param params
     * @return
     */
    @PostMapping("/getKnowledgeListByConditions")
    public R getKnowledgeListByConditions(@RequestBody Map<String, Object> params) {
        Integer pageIndex = (Integer) params.get("pageIndex");
        Integer pageSize = (Integer) params.get("pageSize");
        if (pageIndex == null || pageSize == null) {
            pageIndex = 1;
            pageSize = 5;
        }

        Knowledge knowledge = new Knowledge();
        User author = userService.getUserByUserName((String) params.get("authorname"));
        if (author != null) {
            //根据用户名得到用户信息
            knowledge.setAuthorid(author.getUserid());
        }
        knowledge.setTitle((String) params.get("title"));


        List<KnowledgeUserDto> knowledgeList = knowledgeService.getKnowledgeListByConditions(knowledge, pageIndex, pageSize);
        HashMap<String, Object> map = new HashMap<>();
        map.put("knowledgeList", knowledgeList);
        return R.ok(map);

    }

    /**
     * 获取条数
     *
     * @return
     */
    @GetMapping("/getKnowledgeCount")
    public R getKnowledgeCount() {
        long count = knowledgeService.count();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", count);
        return R.ok(map);
    }

    @PostMapping("/updateCarouselById")
    public R updateCarouselById(@RequestParam("knowledgeid") Integer knowledgeId,
                                @RequestParam("iscarousel") Integer isCarousel) {
        Knowledge knowledge = new Knowledge();
        knowledge.setKnowledgeid(knowledgeId);
        knowledge.setIscarousel(isCarousel);
        knowledge.setUpdatetime(new Date());
        boolean update = knowledgeService.updateById(knowledge);
        return update ? R.ok("修改成功！") : R.error("修改失败！");
    }


    /**
     * 根据id修改心理知识
     *
     * @param knowledge
     * @return
     */
    @PostMapping("/updateKnowledgeById")
    public R updateKnowledgeById(@RequestBody Knowledge knowledge) {
        knowledge.setUpdatetime(new Date());
        try {
            knowledgeService.updateById(knowledge);
        } catch (Exception e) {
            return R.error("修改失败！");
        }

        return R.ok("修改成功！");
    }

    /**
     * 根据id删除心理知识
     *
     * @param knowledgeId
     * @return
     */
    @PostMapping("/deleteKnowledgeById")
    public R deleteKnowledgeById(@RequestParam("knowledgeId") Integer knowledgeId) {

        try {
            knowledgeService.removeById(knowledgeId);
        } catch (Exception e) {
            return R.error("删除失败！");
        }
        return R.ok("删除成功！");

    }
}
